package com.design.game;

import com.design.config.MoveBoxConfig;
import com.design.method.error.SizeError;
import com.design.util.moveBox.Box;
import com.design.util.moveBox.MapMethod;
import com.design.util.moveBox.Next;
import com.design.util.moveBox.Picture;

public class MoveBox {
    private final Box[][] map;
    private int playerX;
    private int playerY;
    public Box[][] getMap() {
        return map;
    }
    public int getPlayerX() {
        return playerX;
    }
    public int getPlayerY() {
        return playerY;
    }
    public MapMethod getMethod(){
        return new MapMethod(this.map,this.playerX,this.playerY);
    }
    public MoveBox(MapMethod method){
        this.map = method.getMap();
        this.playerX = method.getPlayerX();
        this.playerY = method.getPlayerY();
    }
    private Box get(Next next) throws SizeError {
        int x = next.getX();
        int y = next.getY();
        if(!SizeError.isOverFlow(this.map.length,x,y)){
            throw new SizeError(x,y);
        }
        return this.map[y][x];
    }
    private boolean moveBoxNext(Box box) {
        if(!box.equals(new char[]{MoveBoxConfig.CHAR_GROUND,MoveBoxConfig.CHAR_BOX_POINT})){//如果下一个位置不是空 和 .
            return false;
        }
        if(box.equal(' ')){//如果下一个是空则为箱子 否则为 箱子在点上
            box.rename(Picture.getBox());
            return true;
        }
        box.rename(Picture.getBoxInPoint());
        return true;
    }
    private boolean moveBox(Next next) throws SizeError {
        Box box = this.get(next);
        next.run();
        if(!moveBoxNext(this.get(next))){
            return false;
        }
        if(box.equal(MoveBoxConfig.CHAR_BOX)){ //如果是箱子 则为空 否则为.
            box.rename(Picture.getGround());
            return true;
        }
        box.rename(Picture.getBoxPoint());
        return true;
    }
    //超级移动 除了边境哪儿都能走
    public void superMove(int[] direction){
        int moveX = this.playerX + direction[0];
        int moveY = this.playerY + direction[1];
        if(moveX == 0 || moveY == 0 || moveX == this.map.length - 1 || moveY == this.map.length - 1){
            return;
        }
        this.playerY = moveY;
        this.playerX = moveX;
    }
    public void setMap(Picture picture){
        this.map[this.playerY][this.playerX] = new Box(picture,this.playerX,this.playerY);
    }
    public boolean movePlayer(int[] direction) {
        try{
            Next next = new Next(direction,this.playerX,this.playerY);
            next.run();
            Box box = this.get(next);
            if(box.equal(MoveBoxConfig.CHAR_WALL)){ // 如果是墙则返回
                return false;
            }
            if(box.equals(new char[]{MoveBoxConfig.CHAR_GROUND,MoveBoxConfig.CHAR_BOX_POINT})){//如果是 空 点 则移动
                this.playerX += direction[0];
                this.playerY += direction[1];
                return true;
            }
            if(!this.moveBox(next)){
                return false;
            };
            this.playerX += direction[0];
            this.playerY += direction[1];
            return true;
        }catch(SizeError e) {
            e.printStackTrace();
        }
        return false;
    }
//    public void fill(Graphics g){
//        for (Box[] boxes : this.map) {
//            for (int x = 0; x < this.map.length; x++) {
//                Box box = boxes[x];
//                box.fill(g,width);
//            }
//        }
//        Picture.getPlayer().fill(g,this.playerX * this.width,this.playerY * this.width,this.width);
//    }
}
